<template>
  <div class="home">

    <el-card
        style="width: 600px;height: 550px;float: right;margin: 100px 150px;border-radius: 20px;background-color: rgba(255,255,255,0.3)">
      <h2>欢迎来到幸运果商城</h2>
      <el-form :model="user" :rules="rules" ref="user" label-width="70px" style="width: 400px;margin: 50px auto">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="user.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input type="text" v-model="user.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="code('user')">获取验证码</el-button>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input type="text" v-model="user.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nick">
          <el-input type="text" v-model="user.nick" placeholder="请输入昵称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button style="position: relative;right: 30px" type="primary" @click="reg('user')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: {
        username: '',
        password: '',
        nick: '',
        code: '',
        phone: ''
      },
      rules: {
        username: [
          {message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 11, message: '长度在 2 到 11 个字符', trigger: 'blur'}
        ],
        password: [
          {message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 11, message: '长度在 3 到 11 个字符', trigger: 'blur'}
        ],
        nick: [
          {message: '请输入昵称', trigger: 'blur'},
          {min: 2, max: 11, message: '长度在 2 到 11 个字符', trigger: 'blur'}
        ],
        code: [
          {message: '请输入验证码', trigger: 'blur'},
          {min: 0, max: 5, message: '长度在4个字符', trigger: 'blur'}
        ],
        phone: [
          {message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '长度在11个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    code(user) {
      this.$refs[user].validate((vaild) => {
        if (vaild) {
          let url = 'http://localhost:8091/sendSMS/?phone=' + this.user.phone;
          console.log('url = ' + url)
          this.axios.get(url).then((response) => {
            let responseBody = response.data
            if (responseBody.state == 20000) {
              this.$message.success("验证码已发送");
            } else {
              this.$message.error(responseBody.message);
            }
          })
        }
      })
    },
    reg(user) {
      this.$refs[user].validate((vaild) => {
        if (vaild) {
          let url = 'http://localhost:8091/user/add-new';
          console.log('url = ' + url)
          let formData = this.qs.stringify(this.user);
          console.log(this.user);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data
            if (responseBody.state == 20000) {
              this.$message({
                message: '注册成功！,快去登录吧',
                type: 'success'
              });
              location.href = "/login";
            } else {
              this.$message.error('注册失败，用户名已存在！');
            }
          })
        }
      });
    }
  }
}
</script>
<style>
body {
  background-image: url("../../public/img/login.jpg");
  background-repeat: repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
  width: 100%;
  height: 100%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

h2 {
  font-size: 44px;
  color: #0096dc;
  margin-top: 0px;
}
</style>
